﻿using System;


public class TaskCompletionSource
{
    CompletionState state = CompletionState.Pending;
    public Action onCompleted;
    public Action<Exception> onException;
    public CompletionState GetState()
    {
        return state;
    }
    public void SetCancel()
    {
        state = CompletionState.Canceled;
    }

    public void SetException(Exception exception)
    {
        state = CompletionState.Faulted;
        if (this.onException != null)
        {
            this.onException.Invoke(exception);
        }
    }

    public void SetResult()
    {
        state = CompletionState.Succeeded;
        if (this.onCompleted != null)
        {
            this.onCompleted.Invoke();
        }
    }
}

public class TaskCompletionSource<T>
{
    CompletionState state = CompletionState.Pending;
    public Action<T> onCompleted;
    public Action<Exception> onException;
    public CompletionState GetState()
    {
        return state;
    }
    public void SetCancel()
    {
        state = CompletionState.Canceled;
    }

    public void SetException(Exception exception)
    {
        state = CompletionState.Faulted;
        if (this.onException != null)
        {
            this.onException.Invoke(exception);
        }
    }

    public void SetResult(T result)
    {
        state = CompletionState.Succeeded;
        if (this.onCompleted != null)
        {
            this.onCompleted.Invoke(result);
        }
    }

  
}
